<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <div>
      <button @click="axiosGetBtn">请求1</button>
      <button @click="axiosGetBtn2">请求2</button>
      <button @click="axiosPostBtn">请求3</button>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  data() {
    return {
      name: "World",
    };
  },
  methods: {
    async axiosGetBtn() {
      try {
        // 发起 GET 请求，这里以请求 JSONPlaceholder 的示例 API 为例
        const response = await axios.get("http://localhost:3000/users");
        this.responseData = response.data;
        console.log("请求成功:", this.responseData);
      } catch (error) {
        console.error("请求失败:", error);
      }
    },
    async axiosGetBtn2() {
      try {
        // 发起 GET 请求，这里以请求 JSONPlaceholder 的示例 API 为例
        const data = {
          id: 1,
        };
        const response = await axios.get(
          "http://localhost:3000/users/" + data.id
        );
        this.responseData = response.data;
        console.log("请求成功:", this.responseData);
      } catch (error) {
        console.error("请求失败:", error);
      }
    },
    async axiosPostBtn() {
      try {
        const data = {
          name: "cth",
          age: 27,
        };
        // 发起 GET 请求，这里以请求 JSONPlaceholder 的示例 API 为例
        const response = await axios.post("http://localhost:3000/users/", data);
        this.responseData = response.data;
        console.log("请求成功:", this.responseData);
      } catch (error) {
        console.error("请求失败:", error);
      }
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
